//
//  CustomHud.m
//  SeaHome
//
//  Created by mac03 on 2021/3/30.
//

#import "CustomHud.h"
@interface CustomHud()

@end
@implementation CustomHud
+ (instancetype)shareManager {
    
    static CustomHud * manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[CustomHud alloc] init];
    });
    return manager;
}
- (instancetype)init{
    self = [super init];
    if (self) {
        
    }
    return self;
}
- (void)showMessage:(NSString *)message{
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    UIView *showview =  [[UIView alloc]init];
    showview.backgroundColor = [UIColor blackColor];
    showview.frame = CGRectMake(1, 1, 1, 1);
    showview.alpha = 1.0f;
    showview.layer.cornerRadius = 20.0f;
    showview.layer.masksToBounds = YES;
    [window addSubview:showview];
    
    UILabel *label = [[UILabel alloc]init];
    CGSize LabelSize = [message sizeWithFont:[UIFont systemFontOfSize:18] constrainedToSize:CGSizeMake(290, 9000)];
    label.text = message;
    label.textColor = [UIColor whiteColor];
    label.textAlignment = 1;
    label.numberOfLines=100;
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont systemFontOfSize:16];
    [showview addSubview:label];
    
    float w=LabelSize.width+40;
    float h=LabelSize.height+20;
    
    if (h<40) {
        h=40;
    }
    if (w<60) {
        w=60;
    }
    label.frame = CGRectMake(0, 0, w, h);
//    showview.frame = CGRectMake((kScreenWidth - w)/2, 196 + kHeight_NavBar, w, h);
    [showview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(window);
        make.centerY.equalTo(window);
        make.width.mas_offset(w);
        make.height.mas_offset(h);
    }];
    [UIView animateWithDuration:3.0 animations:^{
        showview.alpha = 0;
    } completion:^(BOOL finished) {
        [showview removeFromSuperview];
    }];
}
- (void)showHud{
    self.hud = [MBProgressHUD showHUDAddedTo:[self topViewController].view animated:YES];
    self.hud.removeFromSuperViewOnHide = YES;
    [self.hud showAnimated:YES];
}
- (void)hidenHud{
    if (self.hud) {
        [self.hud hideAnimated:YES];
        self.hud = nil;
    }
   
    
}
/** 根据root级导航条获取 */
- (UIViewController *)topViewController {
    UIViewController *resultVC;
    resultVC = [self _topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]];
    while (resultVC.presentedViewController) {
        resultVC = [self _topViewController:resultVC.presentedViewController];
    }
    return resultVC;
}
- (UIViewController *)_topViewController:(UIViewController *)vc {
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return [self _topViewController:[(UINavigationController *)vc topViewController]];
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        return [self _topViewController:[(UITabBarController *)vc selectedViewController]];
    } else {
        return vc;
    }
    return nil;
}
@end
